// i.r(t), i.d(t, { default: () => SkyboxModule })
import Module from "./Module"
import SymbolList from "../SymbolList"
import * as THREE from "three"
import o from "../shader/31090"
import a from "../shader/69383"
import funcUtil from "../utils/func.util"
import sa from "../shader/13833"
import sb from "../shader/55185"
const c = {
  sky: {
    uniforms: {
      topColor: {
        type: "v3",
        value: new THREE.Vector3(0.094, 0.102, 0.11)
      },
      bottomColor: {
        type: "v3",
        value: new THREE.Vector3(0.2, 0.216, 0.235)
      },
      radius: { type: "f", value: 100 }
    },
    vertexShader: funcUtil.addNR(a, sa),
    fragmentShader: funcUtil.addNR(o, sb)
  }
}
class SkyboxMaterial extends THREE.RawShaderMaterial {
  constructor(e = {}) {
    super(
      Object.assign(
        {
          fragmentShader: c.sky.fragmentShader,
          vertexShader: c.sky.vertexShader,
          uniforms: THREE.UniformsUtils.clone(c.sky.uniforms),
          name: "SkyboxMaterial"
        },
        e
      )
    )
  }
}
import RenderLayers, { RenderLayer } from "../utils/RenderLayers"
import SkySphereMesh from "../mesh/SkySphereMesh"
import p from "../math/63560"
class Skybox {
  scene: any
  cameraData: CameraData
  addToRaycasting: any
  removeFromRaycasting: any
  renderLayer: RenderLayer
  bindings: any[]
  skyVisual: SkySphereMesh
  material: any
  skyCollider: SkySphereMesh
  constructor(e, t, i, n, s = RenderLayers.ALL) {
    ;(this.scene = e), (this.cameraData = t), (this.addToRaycasting = i), (this.removeFromRaycasting = n), (this.renderLayer = s), (this.bindings = [])
  }
  init() {
    const { visualMesh: e, colliderMesh: t } = this.setupSkysphere()
    ;(this.skyVisual = e), (this.material = this.skyVisual.material), (this.skyCollider = t)
  }
  dispose() {
    this.material.uniforms.pano0Map.value.dispose(), this.material.uniforms.pano1Map.value.dispose(), this.material.dispose(), this.skyVisual.geometry.dispose()
  }
  activate(e) {
    this.scene.addChild(this.scene.ids.Root, this.skyVisual),
      this.skyVisual.updateMatrixWorld(!0),
      (this.skyVisual.visible = !0),
      this.addToRaycasting(this.skyCollider, !1)
  }
  deactivate(e) {
    for (const e of this.bindings) e.cancel()
    ;(this.bindings = []),
      this.scene.removeChild(this.scene.ids.Root, this.skyVisual),
      this.scene.removeChild(this.scene.ids.CameraRig, this.skyCollider),
      this.removeFromRaycasting(this.skyCollider)
  }
  updateBackgroundColors(e, t) {
    const i = new THREE.Color(e),
      n = new THREE.Color(t)
    this.material.uniforms.topColor.value.set(i.r, i.g, i.b), this.material.uniforms.bottomColor.value.set(n.r, n.g, n.b)
  }
  beforeRender() {
    this.skyVisual.position.copy(this.cameraData.pose.position), this.skyCollider.position.copy(this.cameraData.pose.position)
  }
  render() {}
  setupSkysphere(e?) {
    const t = new THREE.SphereBufferGeometry(1e4, 5, 5)
    t.computeBoundingBox()
    const i = p.oR.far - 10,
      n = new THREE.SphereBufferGeometry(i, 5, 5)
    n.computeBoundingBox(), e || (e = new SkyboxMaterial({ side: THREE.BackSide }))
    const s = new SkySphereMesh(n, e)
    ;(s.layers.mask = this.renderLayer.mask),
      (s.name = "Skysphere"),
      (s.renderOrder = renderOrderEnum.boundingSkybox),
      s.updateMatrixWorld(!0),
      (e.uniforms.radius.value = i),
      (e.depthWrite = !1),
      (e.depthTest = !1)
    return {
      visualMesh: s,
      colliderMesh: new SkySphereMesh(
        t,
        new THREE.MeshBasicMaterial({
          opacity: 0,
          depthWrite: !1,
          side: THREE.BackSide
        })
      )
    }
  }
}
import SettingsData from "../data/SettingsData"

import CameraData from "../data/CameraData"
import optionsKeyEnum from "../enum/optionsKey.enum"
import renderOrderEnum from "../enum/renderOrder.enum"
import { colorKey, colorList } from "../constant/color.const"

export default class SkyboxModule extends Module {
  skybox: Skybox
  constructor() {
    super(...arguments), (this.name = "skybox-module")
  }
  async init(e, t) {
    const [i, n, r, o] = await Promise.all([
        t.getModuleBySymbol(SymbolList.WEBGL_RENDERER),
        t.market.waitForData(SettingsData),
        t.getModuleBySymbol(SymbolList.INPUT),
        t.market.waitForData(CameraData)
      ]),
      a = t.claimRenderLayer("skybox"),
      l = i.getScene()
    ;(this.skybox = new Skybox(l, o, r.registerMesh, r.unregisterMesh, a)), t.addComponent(this, this.skybox)
    const c = n.tryGetProperty(optionsKeyEnum.BackgroundColor, colorKey.default)
    this.skybox.updateBackgroundColors(colorList[c].bgPrimary, colorList[c].bgSecondary),
      this.bindings.push(
        n.onPropertyChanged(optionsKeyEnum.BackgroundColor, e => {
          this.skybox.updateBackgroundColors(colorList[e].bgPrimary, colorList[e].bgSecondary)
        })
      )
  }
}
